package ru.kest.babymonitor.service.audio;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import ru.kest.babymonitor.R;
import ru.kest.babymonitor.Settings;
import ru.kest.babymonitor.StartActivity;
import ru.kest.babymonitor.service.AudioService;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Konstantin Kharitonov on 04.08.2014.
 *
 */
public class AlarmPlayer {

    private static AlarmPlayer instance;
    private SoundPool soundPool = null;
    private Map<String, Integer> soundsMap = new HashMap<String, Integer>();
    private boolean alarmSoundOn = false;
    private long lastAlarmTime = 0;

    private Settings settings = Settings.getInstance();

    Context context;

    private AlarmPlayer(Context context) {
        this.context = context;
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        soundsMap.put("alarm", soundPool.load(context, R.raw.alarm, 1));
        soundsMap.put("notify", soundPool.load(context, R.raw.notify, 1));
    }

    public static synchronized AlarmPlayer createAlarmPlayer(Context context) {
        instance = new AlarmPlayer(context);
        return instance;
    }

    public static AlarmPlayer getInstance() {
        if (instance == null) {
            throw new RuntimeException("AlarmPlayer is not initialized. Use createAlarmPlayer() before.");
        }
        return instance;
    }

    public synchronized void playAlarm() {
        int alarmId = soundsMap.get("alarm");
        soundPool.play(alarmId, 0.2f, 0.2f, 1, 0, 1);
    }

    public synchronized void playNotify() {
        int notifyId = soundsMap.get("notify");
        soundPool.play(notifyId, 0.5f, 0.5f, 1, 0, 1);
    }

    public void startAlarmSound() {
        long currentTime = System.currentTimeMillis();

        if (!alarmSoundOn) {
            playNotify();
            synchronized (this) {
                alarmSoundOn = true;
                settings.setAudioOnAlarmOn(true);
                lastAlarmTime = currentTime;
            }
            Intent intent = new Intent(context, AudioService.class);
            context.startService(intent);
            if (!StartActivity.running) {
                intent = new Intent(context, StartActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } else {
            synchronized (this) {
                lastAlarmTime = currentTime;
            }
        }
    }

    public synchronized boolean checkIsAlarmTimeoutExpired() {
        if (alarmSoundOn) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - lastAlarmTime > settings.getAlarmPlayingTimeout() * 1000) {
                alarmSoundOn = false;
                settings.setAudioOnAlarmOn(false);
                return true;
            }
        }
        return false;
    }

    public synchronized void stopAlarmSound() {
        if (alarmSoundOn) {
            alarmSoundOn = false;
            settings.setAudioOnAlarmOn(false);
        }
    }

    public boolean isAlarmSoundOn() {
        return alarmSoundOn;
    }
}
